package com.qinglei.recoup.longconn.controller;

import com.qinglei.recoup.common.domain.CommonConstant;
import com.qinglei.recoup.common.domain.RadarMessage;
import com.qinglei.recoup.common.domain.RadarUserDataDTO;
import com.qinglei.recoup.common.domain.RadarUserDataFour;
import com.qinglei.recoup.longconn.entity.MessageV4_1;
import com.qinglei.recoup.longconn.service.RadarDataV4_1Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@Slf4j
public class SengMessageMQ {
    @Autowired
    private RadarDataV4_1Service radarDataV4_1Service;

    @Resource
    RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.host:}")
    private String rabbitmqHost;


    @GetMapping("send1")
    public String send1(Integer b,Integer h,Integer s1,Integer s2) throws InterruptedException {
            if(b==null){
                return "b不能为空";
            }
            if(h==null){
                return "h不能为空";
            }
            RadarMessage radarMessage = new RadarMessage();
            radarMessage.setSn("C45BBECEA6F4");
            radarMessage.setType(1);
            radarMessage.setTimestamp(System.currentTimeMillis());
            radarMessage.setProCode("3_0");
            RadarUserDataDTO radarUserDataDTO = new RadarUserDataDTO();
            radarUserDataDTO.setBreathBpm(b);
            radarUserDataDTO.setHeartBpm(h);
            radarUserDataDTO.setS1(s1);
            radarUserDataDTO.setS2(s2);
            int[] breathPhaseData = {32766, 32768, 32770, 32771, 32772, 32772, 32771, 32770, 32769, 32767} ;
            radarUserDataDTO.setBreathPhaseData(breathPhaseData);
            int[] heartPhaseData = {32751, 32738, 32798, 32793, 32738, 32778, 32763, 32732, 32789, 32771};
            radarUserDataDTO.setHeartPhaseData(heartPhaseData);
            radarMessage.setData(radarUserDataDTO);
            this.sendMessage2MQ(radarMessage);

            return  "OK";
    }


    @GetMapping("send2")
    public String send2(Integer TCFg,Integer OBFg, Integer SFg, Integer TSFg) throws InterruptedException {
        long frameNo = 0;
        frameNo++;
        MessageV4_1 message  = new MessageV4_1();
        RadarMessage radarMessage = new RadarMessage();
        radarMessage.setSn("7C87CE4A117C");
        radarMessage.setType(1);
        radarMessage.setTimestamp(System.currentTimeMillis());
        radarMessage.setProCode("4_0");
        RadarUserDataFour radarUserDataFour = new RadarUserDataFour();
        radarUserDataFour.setSn("7C87CE4A117C");
        radarUserDataFour.setTSFg(TSFg);
        radarUserDataFour.setOBFg(OBFg);
        radarUserDataFour.setSFg(SFg);
        radarUserDataFour.setTCFg(TCFg);
        radarUserDataFour.setAAFg(0);
        radarUserDataFour.setBrebp(13);
        radarUserDataFour.setHRBpRe(71);
        radarUserDataFour.setBs(Arrays.toString(new double[]{-0.909785, -0.888753, -0.866423, -0.841908, -0.814392, -0.783671, -0.750103, -0.714285, -0.676819, -0.637993}));
        radarUserDataFour.setHs(Arrays.toString(new double[]{-0.122269, -0.072465, -0.024232, 0.010745, 0.061644, 0.093207, 0.111819, 0.071349, 0.05407, -0.001539}));
    //    radarUserDataFour.setBs(Arrays.toString(new double[]{0.0, 0.00, 0.00, 0.0, 0.00, 0.00, 0.0, 0.00, 0.00, 0.0}));
    //    radarUserDataFour.setHs(Arrays.toString(new double[]{0.0, 0.00, 0.00, 0.0, 0.00, 0.00, 0.0, 0.00, 0.00, 0.0}));
        radarUserDataFour.setBdef(0.01);
        radarUserDataFour.setHPhERB(16);
        radarUserDataFour.setFrameNo(frameNo);
        radarMessage.setData(radarUserDataFour);
        this.sendMessage2MQ(radarMessage);

        return  "OK";


    }

    /**
     * 发送消息到消息队列
     *
     * @param radarMessage
     */
    private void sendMessage2MQ(RadarMessage radarMessage) {
        if (StringUtils.isNotEmpty(rabbitmqHost)) {
            log.debug("sendMessage2MQ radarMessage: {}", radarMessage);
            rabbitTemplate.convertAndSend(CommonConstant.MQ_RECOUP_DIRECT_EXCHANGE, CommonConstant.MQ_RADAR_DATA_ROUTING, radarMessage);
        }
    }
}
